package com.alpha.core.utils;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.text.StrPool;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;

/**
 * 字符串辅助类
 *
 * @author chenruwan
 * @since 2022-01-01
 */
@Slf4j
@SuppressWarnings("unused")
public class StringUtil {
    /**
     * 字符串常量：{@code "null"} <br>
     * 注意：{@code "null" != null}
     */
    public static final String NULL = CharSequenceUtil.NULL;
    /**
     * 字符串常量：空字符串 {@code ""}
     */
    public static final String EMPTY = CharSequenceUtil.EMPTY;
    /**
     * 字符串常量：点 {@code "."}
     */
    public static final String DOT = StrPool.DOT;
    /**
     * 字符串常量：逗号 {@code ","}
     */
    public static final String COMMA = StrPool.COMMA;
    /**
     * 字符串常量：减号（连接符） {@code "-"}
     */
    public static final String DASHED = StrPool.DASHED;
    /**
     * 字符串常量：斜杠 {@code "/"}
     */
    public static final String SLASH = StrPool.SLASH;
    /**
     * 字符串常量：斜杠 {@code "*"}
     */
    public static final String ASTERISK = "*";

    /**
     * 字符串常量：花括号（左） <code>"{"</code>
     */
    public static final String DELIM_START = "{";

    /**
     * 字符串常量：花括号（右） <code>"}"</code>
     */
    public static final String DELIM_END = "}";
    /**
     * 字符串常量：波浪符 {@code "~"}
     */
    public static final String TILDE = "~";
    /**
     * 字符串常量：小于号（左尖括号） {@code "<"}
     */
    public static final String LESS_THAN = "<";
    /**
     * 字符串常量：大于号（右尖括号） {@code ">"}
     */
    public static final String GREATER_THAN = ">";
    /**
     * 字符串常量：小括号（左） {@code "("}
     */
    public static final String PARENTHESIS_START = "(";
    /**
     * 字符串常量：小括号（右） {@code ")"}
     */
    public static final String PARENTHESIS_END = ")";
    /**
     * 字符串常量：双引号 {@code "\""}
     */
    public static final String QUOTE = "\"";
    /**
     * 字符串常量：空 JSON {@code "{}"}
     */
    public static final String EMPTY_JSON = "{}";
    /**
     * 字符串常量：空数组 {@code "[]"}
     */
    public static final String EMPTY_ARRAY = "[]";

    @Nullable
    public static String valueOf(Object obj) {
        return (obj == null) ? null : obj.toString();
    }

    /**
     * @see CharSequenceUtil#isEmpty(CharSequence)
     */
    public static boolean isEmpty(CharSequence str) {
        return CharSequenceUtil.isEmpty(str);
    }

    /**
     * @see CharSequenceUtil#isNotEmpty(CharSequence)
     */
    public static boolean isNotEmpty(CharSequence str) {
        return CharSequenceUtil.isNotEmpty(str);
    }

    /**
     * @see CharSequenceUtil#isBlank(CharSequence)
     */
    public static boolean isBlank(CharSequence str) {
        return CharSequenceUtil.isBlank(str);
    }

    /**
     * @see CharSequenceUtil#nullToEmpty(CharSequence)
     */
    public static String nullToEmpty(CharSequence str) {
        return CharSequenceUtil.nullToEmpty(str);
    }

    /**
     * @see CharSequenceUtil#nullToDefault(CharSequence, String)
     */
    public static String nullToDefault(CharSequence str, String defaultStr) {
        return CharSequenceUtil.nullToDefault(str, defaultStr);
    }

    /**
     * @see CharSequenceUtil#blankToDefault(CharSequence, String)
     */
    public static String blankToDefault(CharSequence str, String defaultStr) {
        return CharSequenceUtil.blankToDefault(str, defaultStr);
    }

    /**
     * @see CharSequenceUtil#format(CharSequence, Object...)
     */
    public static String format(CharSequence template, Object... params) {
        return CharSequenceUtil.format(template, params);
    }

    /**
     * @see CharSequenceUtil#toUnderlineCase(CharSequence)
     */
    public static String toUnderlineCase(CharSequence str) {
        return CharSequenceUtil.toUnderlineCase(str);
    }

    /**
     * @see CharSequenceUtil#toCamelCase(CharSequence)
     */
    public static String toCamelCase(CharSequence name) {
        return CharSequenceUtil.toCamelCase(name);
    }

    /**
     * 转成大驼峰格式
     */
    public static String toUpperCamel(CharSequence name) {
        return CharSequenceUtil.upperFirst(toCamelCase(name));
    }

    /**
     * 删除首字母（当首字母等于starter）
     */
    public static String removeFirstCharIf(String str, char starter) {
        if (str == null || str.charAt(0) != starter) {
            return str;
        }
        return str.substring(1);
    }

    /**
     * 删除尾字母（当尾字母等于tail）
     */
    public static String stripLastCharIf(String str, char tail) {
        if (str == null || str.charAt(str.length() - 1) != tail) {
            return str;
        }
        return str.substring(0, str.length() - 1);
    }

    /**
     * 切分字符串
     *
     * @see CharSequenceUtil#split(CharSequence, char)
     */
    public static List<String> split(String str, char separator) {
        return isEmpty(str) ? Collections.emptyList() : CharSequenceUtil.split(str, separator);
    }

    /**
     * @see CharSequenceUtil#contains(CharSequence, CharSequence)
     */
    public static boolean contains(CharSequence str, CharSequence searchStr) {
        return CharSequenceUtil.contains(str, searchStr);
    }

    /**
     * Utility classes should not have public constructors
     */
    private StringUtil() {
    }
}
